﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FifaChamps.API.Data;

namespace FifaChamps.API.Logic
{
    public class Tournament
    {
        public int ID { get; set; }
        public string Name { get; set; }
        public string Venue { get; set; }
        public DateTime? StartTime { get; set; }
        public DateTime? EndTime { get; set; }
        List<Stage> Stages { get; set; }

        private Tournament() { }

        private Tournament(int id, string name, string venue, DateTime? startdate, DateTime? enddate)
            : this()
        {
            ID = id;
            Name = name;
            Venue = venue;
            StartTime = startdate;
            EndTime = enddate;
        }

        public int Add()
        {
            if (this.ID == default(int))
            {
                this.ID = DataManager.Add_Tournament(this.Name, this.Venue, (this.StartTime.HasValue) ? this.StartTime.Value : DateTime.Now, 0, this.StartTime, this.EndTime, null);
                return this.ID;
            }

            return 0;
        }

        public static Tournament Factory(string name, string venue, DateTime? startdate, DateTime? enddate)
        {
            return new Tournament(0, name, venue, startdate, enddate);
        }

        private static List<Tournament> Get_TournamentsInternal(string limit)
        {
            var data = Data.DataManager.Get_Tournaments();
            var result = new List<Tournament>();

            switch (limit)
            {
                case "active":
                    data.Values.Where(x => x.EndDate.HasValue).ToList().ForEach(y => result.Add(new Tournament(y.TournamentID, y.Name
                        , y.Venue, y.StartDate, y.EndDate)));
                    break;
                default:
                    data.Values.ToList().ForEach(y => result.Add(new Tournament(y.TournamentID, y.Name
                        , y.Venue, y.StartDate, y.EndDate)));
                    break;
            }

            return result;
        }

        public static List<Tournament> Get_ActiveTournaments()
        {
            return Get_TournamentsInternal("active");
        }

        public static List<Tournament> Get_AllTournaments()
        {
            return Get_TournamentsInternal("all");
        }
    }
}
